package App;

import Covid.enums.ContactHistory;
import Covid.enums.DiagnosisResult;
import Covid.enums.Symptom;
import Engine.CovidDiagnosisEngine;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

// ConsoleApp.java
public class ConsoleApp {
    private static final Scanner scanner = new Scanner(System.in);
    private static final CovidDiagnosisEngine engine = new CovidDiagnosisEngine();

    public static void main(String[] args) {
        printHeader();
        Set<Symptom> symptoms = selectSymptoms();
        Set<ContactHistory> contacts = selectContactHistory();
        Double temperature = symptoms.contains(Symptom.FEVER) ? readTemperature() : null;

        DiagnosisResult result = engine.diagnose(symptoms, contacts, temperature);
        printResult(result);
    }

    private static Set<Symptom> selectSymptoms() {
        System.out.println("\n=== 症状选择 ===");
        return selectOptions(Symptom.values(), "症状");
    }

    private static Set<ContactHistory> selectContactHistory() {
        System.out.println("\n=== 接触史选择 ===");
        return selectOptions(ContactHistory.values(), "接触史");
    }

    private static <T extends Enum<T>> Set<T> selectOptions(T[] values, String typeName) {
        System.out.println("请选择" + typeName + "（输入对应编号，0结束）：");
        for (int i = 0; i < values.length; i++) {
            System.out.printf("%d. %s\n", i+1, getEnumDisplayName(values[i]));
        }

        Set<T> selected = new HashSet<>();
        while (true) {
            System.out.print("选择: ");
            int choice = scanner.nextInt();
            if (choice == 0) break;
            if (choice > 0 && choice <= values.length) {
                selected.add(values[choice - 1]);
            }
        }
        return selected;
    }

    private static String getEnumDisplayName(Enum<?> e) {
        try {
            Method m = e.getClass().getMethod("getChineseName");
            return (String) m.invoke(e);
        } catch (Exception ex) {
            return e.name();
        }
    }

    private static Double readTemperature() {
        System.out.print("\n请输入当前体温(℃): ");
        return scanner.nextDouble();
    }

    private static void printHeader() {
        System.out.println("=================================");
        System.out.println("   新冠肺炎智能问诊系统 v1.0");
        System.out.println("=================================");
    }

    private static void printResult(DiagnosisResult result) {
        System.out.println("\n=== 诊断建议 ===");
        System.out.println(result.getAdvice());
    }
}
